home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Q: How to assign structures?
- Date: 27 Feb 1996 12:51:40 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gvqssINNjl8@anvil.ugrad.cs.ubc.ca>
- References: <4gsalu$dll@ccshst05.cs.uoguelph.ca>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4gsalu$dll@ccshst05.cs.uoguelph.ca>,
- Toby K Hay <thay@uoguelph.ca> wrote:
- >I need to assign values to structures in one array from structures in
- >another array - in essence I'm copying the whole structure from one array
- >to the other. Can I use simple assignment like this:
- >
- >struct StructName {
- > int IVal;
- > float FVal;}
- >
- >struct StructName Array1[5], Array2[5];
- >. . .
- >for (i=0;i<5;i++) Array1[i] = Array2[i];
- >. . .
-
- Yes.
-
- >And if not that, can I use memcpy() and sizeof() to do the assignment:
-
- Absolutely. memcpy(Array2, Array1, sizeof(Array1)); will do. Don't forget to
-
- #include<string.h>
-
- before using memcpy().
-
- >for (i=0;i<5;i++) memcpy(&Array2[i],&Array1[i],sizeof(struct StructName));
- >Or do I have to assign each element of the structure?
-
- No, you don't.
-
- Assigning structures, as well as passing them by value and returning them out
- of functions was blessed years ago by ANSI.
-
- BTW, if you had just _tried_ this instead of asking, you would have found that
- it works. But it's good to ask anyway in case your experiment just happens to
- takes advantage of a gratuitous non-standard feature implemented only in your
- compiler, something which would readily be pointed out by avid contributors to
- this newsgroup should it happen. Check the FAQ as well and have your K&R2 by
- your side always.
- --
-
-